/**
 * Copyright 2012 Brigham Young University
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.byu.nlp.cluster.mom;

import edu.byu.nlp.cluster.AbstractProbabilisticModel;
import edu.byu.nlp.cluster.ProbabilisticModel;
import edu.byu.nlp.data.SparseFeatureVector;

public class MixtureOfMultinomialsModel extends AbstractProbabilisticModel {
	
	private final MoMParameters params;
	
	private MixtureOfMultinomialsModel(MoMParameters params) {
		this.params = params;
	}
	
	/**
	 * Computes logarithm of a term proportional to p(y|x), i.e.,
	 * <pre>
	 *    log p(y|x) - c = log p(y) + \sum_v log p(v | y)^x_iv for each y.
	 * </pre>
	 */
	@Override
	public double[] unnormalizedLogPOfYGivenX(SparseFeatureVector instance) {
		// TODO : decide if the return value should be guaranteed to be a copy.
		return params.logJoint(instance).clone();
	}

	public static ProbabilisticModel newWithParameters(MoMParameters parameters, boolean copyParams) {
		if (copyParams) {
			parameters = parameters.clone();
		}
		return new MixtureOfMultinomialsModel(parameters);
	}

	public double[] getLogPOfY() {
		return params.getLogPOfY();
	}

	public double[][] getLogPOfXGivenY() {
		return params.getLogPOfXGivenY();
	}
	
}